﻿using System.ComponentModel.DataAnnotations;
using CSRedis;
using Microsoft.AspNetCore.Mvc;
using PMS.WebApi.Attributes;
using PMS.WebApi.Common;
using PMS.WebApi.Models;
using Swashbuckle.AspNetCore.Annotations;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace PMS.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [SwaggerTag("控制器", "监控平台接口")]
    public class MonitoringController : ControllerBase
    {
        // POST api/<MonitoringController>        
        [HttpPost]
        [ApiJwtAuthorizationFilter]
        [ApiJwtActionFilter]
        //[ApiAuthorize]
        //[ApiAuth]
        [SwaggerOperation("上报数据", "将采集到的性能数据上报到监控平台")]
        public dynamic Post([FromBody][Required] PC_KPI data)
        {
            try
            {
                //DateTimeOffset now = DateTimeOffset.Now;
                //var timestamp = now.ToUnixTimeSeconds().ToString();
                data.collect_time = DateTime.Now;
                data.app_kpi_list.ForEach((d) =>
                {
                    d.ip = data.ip;
                    d.collect_time = data.collect_time;
                });
                //NewLifeRedis.Instance.SetValue(timestamp, data);
                var csredis = new CSRedisClient(SysConst.redisConnectionStr);
                csredis.LPush("PC_KPI_LIST", data);
                csredis.Dispose();
                return true.ToSuccessResult();
            }
            catch (Exception ex)
            {
                return ex.Message.ToErrorResult();
            }
        }
    }
}
